<?php
!function_exists('adminmsg') && die('Forbidden');

/**
 * 管理内容，添加、编辑。
*/
require_once(R_P.'require/class_content.php');
require_once(R_P.'require/chinese.php');

class Edit extends Content {

	var $mid;
	var $cid;

	public function __construct(){
		global $action;
		!$action && $action='add';
		if (in_array($action, array('add','edit'))) $this->show();
		$this->selectMethod($action);
	}

	protected function show()
	{
		global $action,$cid,$timestamp,$catedb;
		$defaultcid = GetCookie('defaultcid');
		require_once(R_P.'require/class_cate.php');
		$cate = new Cate();
		$cate_select = $cate->tree();
		if(function_exists('adminmsg') && !$defaultcid && !$cid){
			require PrintEot('header');
			require PrintEot('edit');
			adminbottom();
		}
		!$cid && $cid = $defaultcid;

		if($catedb[$cid]['mid']<0){
			Showmsg('pub_cannotadd'); //调用类型的模型不能添加内容
		}elseif ($catedb[$cid]['mid']==0){
			Showmsg('pub_linkadd'); //外部栏目无法添加内容
		}else{
			Cookie('defaultcid',$cid,$timestamp+31536000);
		}

		$this->mid = $catedb[$cid]['mid'];
		$this->cid = $cid;
		$this->catedb = $catedb;
	}

	/**
	 * 方法选择
	 *
	 * @param string $action
	 */
	protected function selectMethod($action){
		switch ($action){
			case 'add':
				$this->addContent();
				break;
			case 'edit':
				$this->editContent();
				break;
			case 'addimage':
				$this->addImage();
				break;
			case 'upload': //编辑的时候上传附件
				$this->uploadAttach();
				break;
			case 'fckupload':
				$this->FckUpload();
				break;
			case 'tags':
				$this->tags();
				break;
			case 'SearchTag':
				$this->SearchTag();
				break;
			case 'uploadRes':
				$this->uploadRes();
				break;
			case 'removeRes':
				$this->removeRes();
				break;
			default:
				$this->addContent();
				break;
		}
	}

	/**
	 * 添加内容
	 *
	 */
	protected function addContent()
	{
		global $step,$action,$basename;
		parent::__construct($this->mid);
		$cid = $this->cid;
		if(!$step)
		{
			require_once(R_P.'require/class_cate.php');
			$cate = new Cate();
			$cate_select = $cate->tree();
			$cate_select = str_replace("value=\"$cid\"","value=\"$cid\" selected",$cate_select);
			$inputArea = $this->inputArea();
			require PrintEot('header');
			require PrintEot('edit');
			adminbottom();
		}
		elseif ($step==2)
		{
			empty($_POST['title']) && Showmsg('pub_notitle');
			$resTitle = $_POST['restitle'];
			$tid = $this->InsertData($_POST, $this->cid);
			$uploadRid = $this->uploadRes($resTitle, $tid, $this->cid);
			$uploadNum = count($uploadRid);
			$this->updateResNum($tid, $uploadNum);
			if ($uploadNum == 0)
			{
				adminmsg('内容信息添加成功，但未能成功上传资源文件');
			}
			else 
			{
				adminmsg('pub_addok');
			}
		}
	}

	/**
	 * 编辑内容
	 *
	 */
	protected function editContent()
	{
		global $step,$tid,$admin_file,$action,$basename;
		$cid = $this->cid;
		parent::__construct($this->mid);
		if(!$_POST['step'])
		{
			require_once(R_P . 'require/class_resource.php');
			$r = new Resource();
			$res = $r->get($tid);
			
			require_once(R_P.'require/class_cate.php');
			
			$cate = new Cate();
			$cate_select = $cate->tree();
			$cate_select = str_replace("value=\"$cid\"","value=\"$cid\" selected",$cate_select);
			
			$inputArea = $this->editArea($tid);
			list($tagsid,$tags) = $this->getTags($tid);
			/*
			$aids = $this->getAttach($tid);
			$aids = implode(',',$aids);
			*/
			require PrintEot('header');
			require PrintEot('edit');
			adminbottom();
		}
		elseif ($_POST['step']==2)
		{
			empty($_POST['title']) && Showmsg('pub_notitle');
			$resTitle = $_POST['restitle'];
			$uploadRid = $this->uploadRes($resTitle, $tid, $this->cid);
			$uploadNum = count($uploadRid);

			$this->updateResNum($tid, $uploadNum);
			$this->UpdateData($_POST, $_POST['tid']);
			/*
			if($GLOBALS['catedb'][$cid]['htmlpub']){
				$tid = intval($_POST['tid']);
				adminmsg('pub_edithtmlok',"$admin_file?adminjob=content&action=whole&cid=$cid&tid=$tid&job=pubupdate");
			}else{
			*/
				adminmsg('pub_editok',"$admin_file?adminjob=content&cid=$this->cid&action=view");
			//}
		}
	}

	/**
	 * 上传一个新资源
	 *
	 * @param unknown_type $resTitle
	 * @param unknown_type $tid
	 * @param unknown_type $cid
	 * @return unknown
	 */
	protected function uploadRes($resTitle, $tid = '', $cid = '')
	{
		require_once(R_P . 'require/class_resource.php');
		$res = new Resource();
		return $res->upload($resTitle, $tid, $cid);
	}
	
	/**
	 * 删除一个资源
	 *
	 */
	protected function removeRes()
	{
		$rid = intval($_GET['rid']);
		$tid = intval($_GET['tid']);
		require_once(R_P . 'require/class_resource.php');
		$res = new Resource();
		$delnum = $res->remove($rid, $tid);
		//$this->updateResNum($tid, $delnum);
		echo '1';
		exit;
	}


	protected function tags(){
		global $db,$tagname;
		$tagname = $this->Convert($tagname);
		$tagname = Char_cv($tagname);
		$tagname = explode(' ',$tagname);
		$tags = '';
		foreach($tagname as $tag){
			if(!$tag){continue;}
			$rs = $db->get_one("SELECT tagid FROM cms_tags WHERE tagname='$tag'");
			if($rs){
				$tags .= $tags ? ','.$rs['tagid'] : $rs['tagid'];
			}else{
				$db->update("INSERT INTO cms_tags SET tagname='$tag'");
				$tags .= $tags ? ','.$db->insert_id() : $db->insert_id();
			}
		}
		echo $tags;
		exit();
	}

	protected function SearchTag(){
		global $db,$tagname;
		$tagname = Char_cv($tagname);
		$tagname = trim(str_replace(array('%','_'),array('\%','\_'),$tagname));
		$rs = $db->query("SELECT * FROM cms_tags WHERE tagname LIKE('$tagname%') ORDER BY num DESC LIMIT 0,10");
		$tmpText = "";
		while ($tag = $db->fetch_array($rs)){
			$i++;
			$tmpText .= "<A href=\"javascript:SelectTag('{$tag[tagname]}','{$tag[tagid]}');\" id=\"tag_{$tag[tagid]}\">{$tag[tagname]}(<font color=red>{$tag[num]}</font>)</A>&nbsp;&nbsp;&nbsp;";
			$i==5 && $tmpText .= '<br/>'; 
		}
		if($tmpText){
			echo $tmpText;
		}else{
			echo 'error';
		}
		exit();
	}

	private function Convert($str){
		global $charset;
		if($charset != 'utf8'){
			$chs = new Chinese('UTF8',$charset);
			$str=$chs->Convert($str);
		}
		return $str;
	}
}

new Edit();
?>